/**
 * 添加或者修改页面
 */
var ProductInfoDlg = {
    data: {
        id: "",
        productNum: "",
        productName: "",
        shelfId: "",
        shelfName: "",
        producePlace: "",
        brand: "",
        carModel: "",
        supplierId: "",
        supplierName: "",
        supplierId: "",
        supplierName: "",
        shopId: "",
        stockCount: "",
        unitPrice: "",
        imgProduct: "",
        remark: "",
        createTime: "",
        createUser: "",
        updateTime: "",
        updateUser: "",
        isDelete: "",
        saledCount: "",
        imgDetails: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;


    //实例化编辑器
    var ue = UE.getEditor('imgProduct', {
        enableAutoSave: false,
        autoHeightEnabled: true,
        autoFloatEnabled: false,
        scaleEnabled: true,         //滚动条
        initialFrameHeight: 400     //默认的编辑区域高度
    });

    //实例化编辑器
    var ueImg = UE.getEditor('imgDetails', {
        enableAutoSave: false,
        autoHeightEnabled: true,
        autoFloatEnabled: false,
        scaleEnabled: true,         //滚动条
        initialFrameHeight: 400     //默认的编辑区域高度
    });

    UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
    UE.Editor.prototype.getActionUrl = function (action) {
        if (action === 'uploadimage' || action === 'uploadscrawl' || action === 'uploadimage') {
            return Feng.ctxPath + '/upload/imgUpdate';
        } else if (action === 'uploadfile') {
            return Feng.ctxPath + '/upload/uploadfile';
        } else if (action === 'uploadvideo') {
            return Feng.ctxPath + '/upload/uploadvideo';
        } else {
            return this._bkGetActionUrl.call(this, action);
        }
    };


    //供应商
    $.ajax({
        url: '/baseCommonSelect/getFactoryNumList',
        dataType: 'json',
        type: 'post',
        success: function(data) {
            if (data!== null) {
                $("#supplierNameSelect").empty();
                $("#supplierNameSelect").append(new Option());
                $.each(data, function(index, item) {
                    $('#supplierNameSelect').append(new Option(item.value,item.id));
                });
            } else {
                $("#supplierNameSelect").append(new Option("暂无数据", ""));
            }
            //重新渲染
            form.render("select");
        }
    });

    //商品分类
    $.ajax({
        url: '/baseCommonSelect/getProductCategory',
        dataType: 'json',
        type: 'post',
        success: function(data) {
            if (data!== null) {
                $("#productCategorySelect").empty();
                $("#productCategorySelect").append(new Option());
                $.each(data, function(index, item) {
                    $('#productCategorySelect').append(new Option(item.value,item.id));
                });
            } else {
                $("#productCategorySelect").append(new Option("暂无数据", ""));
            }
            //重新渲染
            form.render("select");
        }
    });


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {

        //获取选中的值
        var supplierId = $("#supplierNameSelect").val();
        //获取选中文本
        var supplierName = $("#supplierNameSelect").find("option:selected").text();

        //获取选中的值
        var productCategoryId = $("#productCategorySelect").val();
        //获取选中文本
        var productCategoryName = $("#productCategorySelect").find("option:selected").text();

        data.field.supplierId = supplierId;
        data.field.supplierName = supplierName;

        data.field.productCategoryId = productCategoryId;
        data.field.productCategoryName = productCategoryName;

        var ajax = new $ax(Feng.ctxPath + "/product/addItem", function (data) {
            Feng.success("添加成功！");
            // //传给上个页面，刷新table用
            // admin.putTempData('formOk', true);
            // //关掉对话框
            // admin.closeThisDialog();
            window.location.href = Feng.ctxPath + '/product';
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });


    //取消按钮
    $('#cancel').click(function () {
        window.location.href = Feng.ctxPath + "/product";
    });

});